package com.clouddot.baipai.presenter;


import com.clouddot.baipai.contract.ITableFragmentContract;
import com.clouddot.baipai.http.entity.response.getCourseTable.classTime;
import com.clouddot.baipai.http.entity.response.getCourseTable.dayInfo;
import com.clouddot.baipai.http.entity.response.getCourseTable.getCourseTableResponse;
import com.clouddot.baipai.http.entity.response.getCourseTable.weeksName;
import com.clouddot.baipai.infrastructure.environment.Env;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;

import org.wxy.sdk.base.impl.BasePresenterImpl;

import java.util.ArrayList;
import java.util.List;

import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

public class TableFragmentPresenter extends BasePresenterImpl<ITableFragmentContract.IView, ITableFragmentContract.IModel> implements ITableFragmentContract.IPresenter {
    public TableFragmentPresenter(ITableFragmentContract.IView view, ITableFragmentContract.IModel model) {
        super(view, model);
    }

    @Override
    public void getCourseTable(String classId) {
        Subscription subscription = mModel.getCourseTable("QueryTimetable", classId)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<String>() {
                               @Override
                               public void call(String result) {
                                   gsonResult(result);
                               }
                           }
                        , new Action1<Throwable>() {
                            @Override
                            public void call(Throwable throwable) {
                                mView.fail(1, throwable.getMessage());

                            }
                        });
        addSubscription(subscription);
    }

    private void gsonResult(String result) {
        getCourseTableResponse response = new getCourseTableResponse();
        try {
            if (!result.isEmpty()) {
                if (!result.equals("{}")) {
                    JsonObject jsonObject = new JsonParser().parse(result).getAsJsonObject();
                    //获取名字和时间
                    JsonElement TimetableSetting = jsonObject.getAsJsonObject("TimetableSetting");
                    JsonObject Parameters = new JsonParser().parse(TimetableSetting.toString()).getAsJsonObject();
                    String parameters = Parameters.get("Parameters").getAsString();
                    JsonObject Parameter = new JsonParser().parse(parameters).getAsJsonObject();
                    JsonArray WeeksArray = Parameter.get("Weeks").getAsJsonArray(); //获取周期
                    JsonArray ClassesArray = Parameter.get("Classes").getAsJsonArray();//获取上课时间

                    JsonElement TimeTable = jsonObject.getAsJsonObject("TimeTable");
                    JsonObject Data = new JsonParser().parse(TimeTable.toString()).getAsJsonObject();
                    JsonArray TableArray = Data.get("Data").getAsJsonArray(); //获取课程内容

                    if (!WeeksArray.isJsonNull() && !ClassesArray.isJsonNull() && !TableArray.isJsonNull()) {
                        for (JsonElement element : WeeksArray) {
                            response.weeksNames.add((weeksName) new Gson().fromJson(element, new TypeToken<weeksName>() {
                            }.getType()));
                        }
                        for (JsonElement element : ClassesArray) {
                            response.classTimes.add((classTime) new Gson().fromJson(element, new TypeToken<classTime>() {
                            }.getType()));
                        }
                        //获取课程内容
                        for (JsonElement element : TableArray) {
                            List<dayInfo> dayInfos = new ArrayList<>();
                            JsonArray DayInfoArray = element.getAsJsonArray();
                            for (JsonElement element1 : DayInfoArray) {
                                JsonObject Item1 = new JsonParser().parse(element1.toString()).getAsJsonObject();
                                JsonArray ItemArray1 = Item1.get("ItemList").getAsJsonArray();
                                dayInfo dayInfo = new dayInfo();
                                if (ItemArray1.size() > 0) {
                                    JsonElement element2 = ItemArray1.get(0);
                                    JsonObject Item2 = new JsonParser().parse(element2.toString()).getAsJsonObject();
                                    dayInfo.setCourseName(Item2.get("CourseName").getAsString());
                                    dayInfo.setMessage(Item2.get("Message").getAsString());
                                    dayInfo.setTeacherName(Item2.get("TeacherName").getAsString());
                                    dayInfo.setBeginWeek(Item2.get("BeginWeek").getAsString());
                                    dayInfo.setEndWeek(Item2.get("EndWeek").getAsString());
                                    dayInfo.setRemark(Item2.get("Remark").getAsString());
                                    dayInfo.setWeekHours(Item2.get("WeekHours").getAsString());
                                } else {
                                    dayInfo.setCourseName("");
                                    dayInfo.setMessage("");
                                    dayInfo.setTeacherName("");
                                }
                                dayInfos.add(dayInfo);
                            }
                            response.dayInfos.add(dayInfos);
                        }
                    } else {
                        mView.fail(1, "获取的课表数据不完整，请到后台上布完整的课表数据");
                    }
                } else {
                    mView.fail(1, "网络错误，请检查网络");
                }
            } else {
                mView.fail(1, "获取课表失败,请刷卡");
            }
        } catch (Exception e) {
            Env.error(e);
        } finally {
            mView.success(1, response);

        }
    }
}
